<template>
  <views class="permission_manager">
    <div class="permission_manager">
      <viewTitle v-if="$route.query.check.i" :title="'权限分配'" :button="true" :btitle="'添加角色'" @clickBtn="add"></viewTitle>
      <div class="table_list_flex">
        <div class="table_list">
          <el-table :data="tableList" style="width: 100%" border :header-cell-style="getRowClass">
            <el-table-column type="index" label="序号" width="100"></el-table-column>
            <el-table-column prop="role_id" label="ID" width="180"></el-table-column>
            <el-table-column prop="role_name" label="角色名称" width="auto"></el-table-column>
            <el-table-column fixed="right" label="操作" width="300">
              <template slot-scope="scope">
                <el-button v-if="$route.query.check.m" type="primary" @click="edit(scope.row)" icon="el-icon-edit">修改</el-button>
                <el-button v-if="$route.query.check.d" type="danger" @click="deleted(scope.row)" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="flex flexR paging">
            <el-pagination
              style="background:#fff; padding:10px 0"
              background
              layout="prev, pager, next,sizes,total, jumper"
              @size-change="(page_size) => {this.pagination.page_size = page_size;this.getData()}"
              @current-change="(page_size) => {this.pagination.page_start = page_size;this.getData()}"
              :page-sizes="[10, 20, 30, 50]"
              :page-size="pagination.page_size"
              :total="pagination.total_count"
              :current-page="pagination.current_page"
              :page-count="pagination.page_count"
            ></el-pagination>
          </div>
        </div>
      </div>
    </div>
  </views>
</template>

<script>
export default {
  data() {
    return {
      tableList: [],
      pagination: {
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      }
    };
  },
  created() {},
  mounted() {
      this.getData();
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    //   新增
    add() {
      this.$toRoutePath({
        path: "/permission_manager/permission_manager_add",
        edit: true,
        query: {}
      });
    },
    //   修改
    edit(row){
      this.$toRoutePath({
        path: "/permission_manager/permission_manager_amend",
        edit: true,
        query: {role_id:row.role_id}
      });      
    },
    //   获取数据
    getData() {
      let post_data={
        pagination:this.pagination
      }
       this.$raxios.post(
            this.$mapapi.admin_listRole,
            {data:post_data}
        ).then(res=>{
          if(res.code==200){
           this.tableList = res.data.list;
           this.pagination = res.data.pagination;
          }else{
            this.tableList = [];
            this.pagination = [];
	          this.$message.error(res.msg)
            return;
          }
        })      
    },
    deleted(row) {
      this.$confirm("此操作将删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
            let post_data={
              role_id:row.role_id
            }
           this.$raxios.post(
                this.$mapapi.admin_delRole,
                {data:post_data}
            ).then(res=>{
              if(res.code==200){
                 this.$message({type: "success", message: "删除成功!"});
                 this.getData();
              }else{
                 this.$message.error(res.msg);
                return;
              }
            })    
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style lang="less"  scope="true"   src="@/style/system_manager/permission_manager.less">
</style>
